﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace MaxZhang.EasyEntities.EasyDesigner.Core
{
    [Serializable]
    public class DbEntity
    {
        public DbEntity()
        {
            Propertys = new ObservableCollection<DbProperty>();
        }


        [XmlAttribute("Name")]
        public string Name { set; get; }
        [XmlAttribute("Memo")]
        public string Memo { set; get; }

        [XmlAttribute("Keys")]
        public string Keys { set; get; }

        [XmlArray("Propertys")]
        public ObservableCollection<DbProperty> Propertys { set; get; }

        [XmlAttribute("IsConfig")]
        [DefaultValue(false)]
        public bool IsConfig { set; get; }

        public string GetTableName()
        {
            return Pn(Name);
        }

        public string GetKeys()
        {
            var keys = Propertys.Where(p => p.IsPrimaryKey).Select(p=>string.Format("\"{0}\"",p.Name) );
            return string.Join(",",keys);
        }

        public static string Pn(string name)
        {
            StringBuilder sb =new StringBuilder();
            var names = name.Split('_');
            foreach (var s in names)
            {
                var tInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
                string nn = tInfo.ToTitleCase(s);
                sb.Append(nn);
            }
            return sb.ToString();
        }
    }
}
